/*
	Copyright (c) 2010 Canadensys
*/
package net.canadensys.dataportal.vascan;
import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import net.canadensys.dataportal.utils.HibernateUtil;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@BatchSize(size = 1000)
@Table(name="taxonomy")
@Cache(region="Taxonomy", usage = CacheConcurrencyStrategy.READ_WRITE)
public class Taxonomy implements Serializable{
	private static final long serialVersionUID = -2962667867825974317L;
	private int			id;
	private Taxon		child;
	private Taxon		parent;
	
	/**
	 * @return the id
	 */
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public int getId() {
		return id;
	}
	
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	
	@ManyToOne
	@JoinColumn(name="taxonid",insertable=false,updatable=false)
	public Taxon getChild(){
		return child;
	}
	
	public void setChild(Taxon child){
		this.child = child;
	}
	
	@ManyToOne
	@JoinColumn(name="taxonid",insertable=false,updatable=false)
	public Taxon getParent(){
		return parent;
	}
	
	public void setParent(Taxon parent){
		this.parent = parent;
	}
	
	public boolean save(){
		boolean success = false;
		org.hibernate.Session hibernateSession = HibernateUtil.getSessionFactory().getCurrentSession();
		hibernateSession.beginTransaction();
		try{
			hibernateSession.saveOrUpdate(this);
			success = true;
		}
		catch(Exception e){
			//this.error = e.getMessage();
		}
		return success;
	}
	
	/**
	 * 
	 * @return
	 */
	public boolean delete(){
		boolean success = false;
		org.hibernate.Session hibernateSession = HibernateUtil.getSessionFactory().getCurrentSession();
		hibernateSession.beginTransaction();
		try{
			hibernateSession.delete(this);
			success = true;
		}
		catch(Exception e){
			//this.error = e.getMessage();
		}
		return success;
	}
}
